package logic

import (
	"context"
	"fmt"
	"zhihu/mobels/mysql"

	"zhihu/zhihurpc/internal/svc"
	"zhihu/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddAnswerLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddAnswerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddAnswerLogic {
	return &AddAnswerLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddAnswerLogic) AddAnswer(in *zhihurpc.AddAnswerRequest) (*zhihurpc.AddAnswerResponse, error) {
	// todo: add your logic here and delete this line
	var answer mysql.Answer
	answer = mysql.Answer{
		UserId:    in.UserId,
		ProblemId: in.ProblemId,
		Content:   in.Content,
	}
	err := mysql.DB.Create(&answer).Error
	if err != nil {
		return nil, fmt.Errorf("评论失败")
	}
	return &zhihurpc.AddAnswerResponse{AnswerId: int64(answer.ID)}, nil
}
